package dao;
import java.util.List;

import org.hibernate.Transaction;
import org.hibernate.classic.Session;

import util.HibernateUtil;
import entity.ItemOrdemServico;
import entity.ItemParcela;
import entity.TipoProduto;

public class ItemOrdemServicoDaoImplementation implements ItemOrdemServicoDao {

	public void save(ItemOrdemServico itemOrdemServico) 
	{
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = session.beginTransaction();
		session.save(itemOrdemServico);
		transaction.commit();
		session.close();
	}

	public void update(ItemOrdemServico itemOrdemServico) 
	{
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = session.beginTransaction();
		session.update(itemOrdemServico);
		transaction.commit();
	}

	public void delete(ItemOrdemServico itemOrdemServico) 
	{
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = session.beginTransaction();
		session.delete(itemOrdemServico);
		transaction.commit();
	}

	public void consult(ItemOrdemServico itemOrdemServico) 
	{
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = session.beginTransaction();
		itemOrdemServico = (ItemOrdemServico) session.load(ItemOrdemServico.class, new String("ItemOrdemServico"));
	}

	public ItemOrdemServico getItemOrdemServico(long id) 
	{
		Session session = HibernateUtil.getSessionFactory().openSession();
		return (ItemOrdemServico) session.load(ItemOrdemServico.class, id);
	}
	
	public List<ItemOrdemServico> listOrdemServico() 
	{
	Session session = HibernateUtil.getSessionFactory().openSession();
	Transaction transaction = session.beginTransaction();
	String sql = "SELECT new ItemOrdemServico( i.idItemOrdemServico, i.quantidade, i.produto) FROM ItemOrdemServico i";
	List<ItemOrdemServico> lista = session.createQuery(sql).list();
	transaction.commit();
	session.close();
	return lista;
	}
	
	@Override
	public List<ItemOrdemServico> list() {
		Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction transaction = session.beginTransaction();
        String sql = "FROM ItemOrdemServico WHERE ordemServico="+null;
        List<ItemOrdemServico> listaOrdemServico = session.createQuery(sql).list();
        transaction.commit();
        session.close();
        return listaOrdemServico;
	}
	

}
